/**
 * XmlColumnAddDialog.java : [com.shift.eclipse.converter.dialog] Created on 2008. 12. 17.
 * 
 * Copyright (c) 2000-2008 Shift Information & Communication Co.
 * 5F, Seongsu Venture town, 231-1, Seongsu2-Ga Seongdong-Gu, Seoul, Korea 133-826
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Shift Information & Communication Co. ("Confidential Information").
 * You shall not disclose such Confidential Information and shall use 
 * it only in accordance with the terms of the license agreement you 
 * entered into with Shift Information & Communication.
 */
package com.shift.converter.wizard;

import org.eclipse.jface.dialogs.TrayDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Item;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.TreeItem;

public class XmlColumnAddDialog extends TrayDialog {

	private Shell shell;
	private String columnName = "";
	private String columnXpath = "";
	private String columnSize = "";
	private String columnType = "";

	private Text nameText;
	private Combo typeCombo;
	private Text sizeText;

	private final String DIALOG_TITLE = "Column Mapping";
	private final String COLUMN_NAME = "Column Name: ";
	private final String DATA_TYPE = "Data Type: ";
	private final String DATA_SIZE = "Data Size: ";

	private final String[] TYPE_LIST = { "STRING", "INT", "LONG", "DOUBLE" };

	public XmlColumnAddDialog(Shell parentShell, Item item) {
		super(parentShell);
		this.shell = parentShell;
		if(item instanceof TreeItem) {
			this.columnXpath = (String)item.getData("xpath");
			String tmp = item.getText();
			if (tmp.indexOf("@") > -1)
				tmp = tmp.substring("@".length(), tmp.length());
			this.columnName = tmp;
			columnType = TYPE_LIST[0];
			columnSize = "255";
		} else if(item instanceof TableItem) {
			columnName = ((TableItem) item).getText(0);
			columnXpath = ((TableItem) item).getText(1);
			columnType = ((TableItem) item).getText(2);
			columnSize = ((TableItem) item).getText(3);
		}
		setShellStyle(SWT.DIALOG_TRIM | SWT.RESIZE | SWT.APPLICATION_MODAL);
	}

	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setText(DIALOG_TITLE);
	}

	protected Control createDialogArea(Composite parent) {

		Composite container = new Composite(parent, SWT.NULL);

		final GridLayout layout = new GridLayout();
		layout.numColumns = 2;
		layout.horizontalSpacing = 2;
		container.setLayout(layout);

		GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
		final Label nameLaber = new Label(container, SWT.NONE);
		nameLaber.setText(COLUMN_NAME);
		nameLaber.setLayoutData(gd);

		gd = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
		gd.widthHint = 100;
		nameText = new Text(container, SWT.BORDER);
		nameText.setLayoutData(gd);

		gd = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
		final Label typeLabel = new Label(container, SWT.NONE);
		typeLabel.setText(DATA_TYPE);
		typeLabel.setLayoutData(gd);

		gd = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
		typeCombo = new Combo(container, SWT.BORDER | SWT.READ_ONLY);
		for (int i = 0; i < TYPE_LIST.length; i++)
			typeCombo.add(TYPE_LIST[i]);
		typeCombo.setLayoutData(gd);

		gd = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
		final Label sizeLabel = new Label(container, SWT.NONE);
		sizeLabel.setText(DATA_SIZE);
		sizeLabel.setLayoutData(gd);

		gd = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
		sizeText = new Text(container, SWT.BORDER);
		sizeText.setLayoutData(gd);

		initColumnData();
		return super.createDialogArea(parent);
	}
	
	private void initColumnData() {
		nameText.setText(columnName);
		typeCombo.setText(columnType);
		sizeText.setText(columnSize);
	}

	protected void okPressed() {
		columnName = nameText.getText();
		columnType = typeCombo.getText();
		columnSize = sizeText.getText();
		super.okPressed();
	}

	public String getColumnName() {
		return columnName;
	}

	public String getColumnType() {
		return columnType;
	}

	public String getColumnSize() {
		return columnSize;
	}
	
	public String getColumnXpath() {
		return columnXpath;
	}
}
